var page = 1;
var limit = 15;
var aaa=99;
function setContryList(data) {
    let html = '<option value="">请选择区/县</option>';
    for (let i = 0; i < data.length; i++) {
        html += '<option value="'+data[i].regiid+'">'+data[i].ccname+'</option>'
    }
    // html+='</ul>';
    $("#selectContry").html(html);
    renderForm();
}

layui.use(['form'], function () {
    var form = layui.form;
    form.on('select(test)', function (data) {
        //TODO执行自己的代码
        let regiid=data.value;
        $.ajax({
            url: zuulhost+'/region/findLow',
            // headers:{"Authorization":token},
            data: {regiid},
            type: 'get',
            dataType: 'json',
            success: function (data) {
                console.log(data,11111);
                setCityList(data.data);
            }
        })
    });
    form.on('select(test1)', function (data) {
        //TODO执行自己的代码
        let regiid=data.value;
        $.ajax({
            url: zuulhost+'/region/findLow',
            // headers:{"Authorization":token},
            data: {regiid},
            type: 'get',
            dataType: 'json',
            success: function (data) {
                console.log(data,11111);
                setContryList(data.data);
            }
        })
    });
});
function keepSession(sessionId) {
    sessionStorage.setItem("sessionId",sessionId);
}
function setPlaceList(data) {
    let html = '';
    for (let i = 0; i < data.length; i++) {
        html += '<a href="proDetail.html" onclick="keepSession('+data[i].statid+')">\n' +
            '    <dl>\n' +
            '    <dt><img src="'+data[i].topimg+'"></dt>\n' +
            '    <dd>'+data[i].stname+' </dd>\n' +
            '    <dd>'+data[i].prices+'</dd>\n' +
            '    </dl>\n' +
            '    </a>';
    }
    // html+='</ul>';
    $("#tbody").html(html);
}

function renderForm() {
    layui.use('form',function () {
        layui.form.render('select');
    })
}

function setCityList(data) {
    let html = '<option value="">请选择市</option>';
    for (let i = 0; i < data.length; i++) {
        html += '<option value="'+data[i].regiid+'">'+data[i].ccname+'</option>'
    }
    // html+='</ul>';
    $("#selectCity").html(html);
    renderForm();
}


function findAllPlace(curPage,pageSize) {
    let prices=$("#prices").val();
    let stname=$("#stname").val();
    let addrid=$("#selectContry").val();
    let maxPri=$("#maxPri").val();
    let minPri=$("#minPri").val();
    let maxAre=$("#maxAre").val();
    let minAre=$("#minAre").val();
    let ooorrr=aaa;
    $.ajax({
        url: zuulhost+'/place/findAll',
        // headers:{"Authorization":token},
        data: {page,limit,addrid,stname,prices,maxPri,minPri,maxAre,minAre,aaa},
        type: 'get',
        dataType: 'json',
        success: function (data) {
            console.log(data,11111);
            setPlaceList(data.data.list);
            console.log(data.data.count)
            pageStart(data.data.count);
        }
    });
}

function findNewPlace(page,limit) {
    let prices=$("#prices").val();
    let stname=$("#stname").val();
    let addrid=$("#selectContry").val();
    let maxPri=$("#maxPri").val();
    let minPri=$("#minPri").val();
    let maxAre=$("#maxAre").val();
    let minAre=$("#minAre").val();
    let ooorrr=aaa;
    console.log(ooorrr,"avc")

    $.ajax({
        url: zuulhost+'/place/findAll',
        // headers:{"Authorization":token},
        data: {page,limit,addrid,stname,prices,maxPri,minPri,maxAre,minAre,aaa},
        type: 'get',
        dataType: 'json',
        success: function (data) {
            console.log(data,11111);
            setPlaceList(data.data.list);
        }
    });
}

function setProvince(data) {

    let html = '<option value="">请选择省</option>';
    for (let i = 0; i < data.length; i++) {
        html += '<option value="'+data[i].regiid+'">'+data[i].ccname+'</option>'
    }
    console.log(html);
    // html+='</ul>';
    $("#select").html(html);

}


function findProvince() {
    $.ajax({
        url: zuulhost+'/region/findProvince',
        // headers:{"Authorization":token},
        type: 'get',
        dataType: 'json',
        success: function (data) {
            setProvince(data.data);
        }
    });
}
/*function changeOrder() {
    if (order==0){
       order=1;
    }else{
       order=0;

    }
    orderByPrices();
}*/
function orderByPrices() {
    // console.log(order);
    if (aaa===0){
        // document.getElementById('#prices1').innerHTML="价格⬆";
        $('#prices1').html("价格⬆");
    }
    if (aaa===1){
        // document.getElementById('#prices1').innerHTML="价格⬇";
        $('#prices1').html("价格⬇");

    }
    findAllPlace();

}

$(function () {
    // findAllPlace();
    findProvince();
    orderByPrices();
});

function pageStart(data){
    layui.use(['laypage', 'layer'], function() {
        var laypage = layui.laypage
            , layer = layui.layer;

        laypage.render({
            elem: 'page'
            ,count: data
            ,limit: 15
            ,first: '首页'
            ,last: '尾页'
            ,layout: ['count', 'prev', 'page', 'next', 'skip']
            ,jump: function(obj){
                console.log(obj);
                curPage = obj.curr;
                pageSize = obj.limit;
                findNewPlace(curPage,pageSize);
            }
        });
    });
}